package com.ccl.iot.updater;import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpHelper {
	public interface HttpDownloadListener{
		public void onDownloadProgress(int aSize, int aPos);
	}
	
	public static HttpURLConnection GetHttpRequestConnection(String aURL){
		if (aURL != null){
			try {
				URL tURL = new URL(aURL);
				HttpURLConnection tConn = (HttpURLConnection)tURL.openConnection();
				int responseCode = tConn.getResponseCode();
				if (responseCode == HttpURLConnection.HTTP_OK){
					return tConn;
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	public static InputStream GetHttpRequestStream(String aURL){
		HttpURLConnection tConn = GetHttpRequestConnection(aURL);
		
		if (tConn != null){
			try {
				return tConn.getInputStream();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return null;  
	}
	
	public static String GetHttpRequestString(String aURL){
		InputStream tStream = GetHttpRequestStream(aURL);
		
		if (tStream != null){
			StringBuilder tString = new StringBuilder();
			
			try {
				BufferedReader tReader = new BufferedReader(new InputStreamReader(tStream,"utf-8"));
				String tLine = null;
				
				while ((tLine = tReader.readLine()) != null){
					tString.append(tLine);  
				}
				
				tStream.close();
				
				if (tString.length() > 0){
					return tString.toString();
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	/**
	 * 
	 * @param aURL
	 * @param aPath
	 * @param aListener
	 * @return Size of the file or -1 mean error
	 */
	public static int GetHttpRequestFile(String aURL, String aPath, HttpDownloadListener aListener){
		HttpURLConnection tConn = GetHttpRequestConnection(aURL);
		
		if (tConn != null){
			int tSize = tConn.getContentLength();
			int tPos = 0;
			
			if (tSize == -1 || tSize > 0){
				InputStream tStream = null;
				
				if (tSize > 0 && aListener != null){
					aListener.onDownloadProgress(tSize, 0);
				}
				
				try {
					tStream = tConn.getInputStream();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				if (tStream != null){
					File tFile = new File(aPath);
					OutputStream tOutput = null;
					
					try {
						tOutput = new FileOutputStream(tFile);
						byte tBuffer[] = new byte[8192];
						int tLength;
						
						while ((tLength = tStream.read(tBuffer)) != -1){
							tOutput.write(tBuffer, 0, tLength);
							
							tPos += tLength;
							if (aListener != null){
								aListener.onDownloadProgress(tSize == -1 ? tPos : tSize, tPos);
							}
						}
						
						if (tLength == -1){
							tSize = tPos;
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
					
					if (tOutput != null){
						try {
							tOutput.close();
						} catch (IOException e) {
						}
					}
				}
				
				if (tStream != null){
					try {
						tStream.close();
					} catch (IOException e) {
					}
				}
				
				if (tSize == tPos){
					return tSize;
				}
			}
		}
		
		return -1;
	}
}
